package com.aletto.general_store.ui.activities.orders;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.aletto.general_store.presenters.ConsumerOrderPageTemplate;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.Objects;


public class ConsumerOrderFragment extends Fragment {

    public static ConsumerOrderFragment newInstance(int type) {
        Bundle bundle = new Bundle();
//        bundle.putString("KEYWORD", keyword);  String keyword,
        bundle.putInt("TYPE", type);
        ConsumerOrderFragment fragment = new ConsumerOrderFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    private int                type;
    private RecyclerView       recycler_view;
    private SmartRefreshLayout refresh_layout;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(com.sisyphus.spinach.R.layout.sisyphus_template_refresh_recycler_view, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recycler_view = view.findViewById(com.sisyphus.spinach.R.id.recycler_view);
        refresh_layout = view.findViewById(com.sisyphus.spinach.R.id.refresh_layout);

        ConsumerOrderPageTemplate template = new ConsumerOrderPageTemplate(recycler_view, refresh_layout);

        Bundle arguments = getArguments();
        if (arguments != null) {
            type = arguments.getInt("TYPE");
            template.setParams(type, null);
            template.execute(getLifecycle());

        } else {

            template.execute(getLifecycle());
        }
    }


    public void request(String keyword) {
        try {

            ConsumerOrderPageTemplate template = new ConsumerOrderPageTemplate(recycler_view, refresh_layout);
            template.setParams(type, keyword);
            template.execute(getLifecycle());

        } catch (Exception e) {
            Log.e("STORE_ORDER", Objects.requireNonNull(e.getLocalizedMessage()));
        }
    }

}
